package com.cn.learn.jpa.service.impl;

import com.cn.learn.jpa.domain.entity.User;
import com.cn.learn.jpa.domain.model.ResultModel;
import com.cn.learn.jpa.mapper.UserMapper;
import com.cn.learn.jpa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description
 * @Author JiJiang
 * @Date 2022/9/13 11:18
 * @Version 1.0
 */

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void save() {

        System.out.println("UserServiceImpl save...");
    }

    @Override
    public ResultModel getUserById(Integer id) {

        User foundUser = null;

        //region  使用MyBatis进行数据查询
        User user = new User();
        user.setId(id);
        foundUser = userMapper.selectById(user);

        //endregion


        ResultModel resultModel = ResultModel.getResultModel(foundUser);

        return resultModel;
    }

    @Override
    public ResultModel getAllUsers(User user) {

        List<User> userList = userMapper.selectUsersByConditions(user);

        int count = userMapper.selectTotalCountByConditions(user);

        return ResultModel.getResultModel(count, userList);
    }

    @Override
    public ResultModel saveAUser(User user) {

        int affectedRow = 0;

        affectedRow = userMapper.insertAUser(user);

        return ResultModel.getResultModel(affectedRow, affectedRow > 0 ? "success" : "fail");

    }

    @Override
    public ResultModel removeUserById(Integer id) {

        User user = new User();
        user.setId(id);

        int affectedRow = userMapper.deleteById(user);

        return ResultModel.getResultModel(affectedRow, affectedRow > 0 ? "success" : "fail");
    }

    @Override
    public ResultModel modifyUserById(User user) {

        int affectedRow = userMapper.updateById(user);

        return ResultModel.getResultModel(affectedRow, affectedRow > 0 ? "success" : "fail");

    }


}
